package Util;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 
 * @author Hua Luo
 * 
 *         The type definition of message
 * 
 *         type meaning 1 login message 2 instant message from client to server
 *         3 instant message from server to client 4 userlist 5
 *         serverclosingmessage 6 clientclosing 7 clientclosing confirm
 * 
 */
public class XMLUtil {
	// 供本类使用的XML构造方法
	private static Document constructDocument() {
		Document document = DocumentHelper.createDocument();
		Element root = DocumentHelper.createElement("message");
		document.setRootElement(root);
		return document;
	}

	// 构造登录结果数据格式
	public static String constructResponse(String result) {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("8");
		Element toReturn = document.getRootElement().addElement("result");
		toReturn.setText(result);
		return document.asXML();
	}

	// 构造客户端TO服务器的消息
	public static String constructC2SMessageXML(String userName, String strMessage) {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("2");
		Element user = document.getRootElement().addElement("user");
		user.setText(userName);
		Element content = document.getRootElement().addElement("content");
		content.setText(strMessage);
		return document.asXML();
	}

	// 构造服务器TO客户端的消息
	public static String constructS2CMessage(String message) {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("3");
		Element content = document.getRootElement().addElement("content");
		content.setText(message);
		return document.asXML();
	}

	// 构造客户端To客户端的消息
	public static String constructC2CMessage(String userName, String revicer, String content) {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("9");
		Element user = document.getRootElement().addElement("user");
		user.setText(userName);
		Element re_user = document.getRootElement().addElement("re_user");
		re_user.setText(revicer);
		Element msg = document.getRootElement().addElement("content");
		msg.setText(content);
		return document.asXML();
	}

	// 构造用户关闭信息
	public static String constructClientClosingConfirm() {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("7");
		Element content = document.getRootElement().addElement("content");
		content.setText("You Can Close");
		return document.asXML();
	}

	// 构造登录信息XML
	public static String constructLoginXML(String userName) {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("1");
		Element user = document.getRootElement().addElement("user");
		user.setText(userName);
		return document.asXML();
	}

	// 构造用户列表
	public static String constructUserList(Set<String> allUsers) {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("4");
		for (String userName : allUsers) {
			Element e = document.getRootElement().addElement("userName");
			e.setText(userName);
		}
		return document.asXML();
	}
	
	//
	//构造服务器文本域XML
	public static String constructServerTextAreaXML(String textArea) {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("10");
		Element content = document.getRootElement().addElement("content");
		content.setText(textArea);
		return document.asXML();
	}

	// 构造服务器关闭信息
	public static String constructServerClosingXML() {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("5");
		return document.asXML();
	}

	// 构造客户端关闭信息
	public static String constructClientClosingXML(String userName) {
		Document document = constructDocument();
		Element type = document.getRootElement().addElement("type");
		type.setText("6");
		Element user = document.getRootElement().addElement("user");
		user.setText(userName);
		return document.asXML();
	}

	// 从客户端登录所发送的XML数据中解析出用户名
	public static String extractUsername(String xml) {
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new StringReader(xml));
			Element user = document.getRootElement().element("user");
			return user.getText();
		} catch (DocumentException e) {
		}
		return null;
	}

	// 从客户端发送出来的XML数据中解析出接受人
	public static String extractReciver(String xml) {
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new StringReader(xml));
			Element re_user = document.getRootElement().element("re_user");
			return re_user.getText();
		} catch (DocumentException e) {
		}
		return null;
	}

	// 解析登录结果
	public static String extractResult(String xml) {
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new StringReader(xml));
			Element result = document.getRootElement().element("result");
			return result.getText();
		} catch (DocumentException e) {
		}
		return null;
	}

	// 解析出用户列表
	public static List<String> extractAllUsers(String xml) {
		List<String> list = new ArrayList();
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new StringReader(xml));
			for (Iterator it = document.getRootElement().elementIterator("userName"); it.hasNext();) {
				Element e = (Element) it.next();
				list.add(e.getText());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return list;
	}

	// 解析出类型
	public static String extractType(String xml) {
		String messageType;
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new StringReader(xml));
			Element root = document.getRootElement();
			Element type = root.element("type");
			messageType = type.getText();
			return messageType;
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 解析信息内容
	public static String extractContent(String message) {
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new StringReader(message));
			Element root = document.getRootElement();
			Element content = root.element("content");
			return content.getText();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}
}